package com.example.e

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.e.Fragment.DiscoverFragment
import com.example.e.Fragment.HomeFragment
import com.example.e.Fragment.MeFragment
import com.example.e.Fragment.OrderFragment
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        title = resources.getString(R.string.home)
        loadFragment(HomeFragment())

        bottomNavigation.setOnNavigationItemSelectedListener{
            when(it.itemId){
                R.id.nav_home -> {
                    title = resources.getString(R.string.home)
                    loadFragment(HomeFragment())
                    return@setOnNavigationItemSelectedListener true
                }

                R.id.nav_discover -> {
                    title = resources.getString(R.string.discover)
                    loadFragment(DiscoverFragment())
                    return@setOnNavigationItemSelectedListener true
                }

                R.id.nav_order -> {
                    title = resources.getString(R.string.order)
                    loadFragment(OrderFragment())
                    return@setOnNavigationItemSelectedListener true
                }

                R.id.nav_me -> {
                    title = resources.getString(R.string.me)
                    loadFragment(MeFragment())
                    return@setOnNavigationItemSelectedListener true
                }

            }
            false
        }


    }
    private fun loadFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.container, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    /*private fun initView() {
        // 初始化控件对象
        val mBtMainLogout =
            findViewById<Button>(R.id.bt_main_logout)
        // 绑定点击监听器
        mBtMainLogout.setOnClickListener(this)
    }

    fun onClick(view: View) {
        if (view.id == R.id.bt_main_logout) {
            val intent = Intent(this, LoginActivity::class.java)
            startActivity(intent)
            finish()
        }
    }*/

}